<template>
	<view class="container">
		<view class="content_box">
			<view class="merchant_enter">
				<view class="cell_box">
					<u-notice-bar mode="horizontal" type="primary" :is-circular="true" :list="noticelist"></u-notice-bar>
					
					<view class="form">
					    <view class="form-item">
					        <view class="label">姓名</view>
					        <input v-model="form.name" :placeholder-style="placeholderStyle" type="text" placeholder="请填写姓名" />
					    </view>
					    <view class="form-item">
					        <view class="label">手机号码</view>
					        <input v-model="form.mobile" :placeholder-style="placeholderStyle" type="number" placeholder="请填写手机号码"></input>
					    </view>
					   <view  @tap="showRegion = true">
							<view class="form-item row">
								<view class="label">所在地区</view>
								<view class="region" :style="{color:form.region?'#1D1F24':'#808080'}">{{form.region?form.region:'请选择所在地区'}}</view>
								<image class="icon-sm ml10" src="/static/images/arrow_right.png" />
							</view>
						</view>
						<view class="form-item">
						    <view class="label">门店名称</view>
						    <input  v-model="form.business_name" :placeholder-style="placeholderStyle" type="text" placeholder="请填写门店名称"></input>
						</view>
					    <view>
					        <u-field 
					        v-model="form.address" 
					        type="textarea"
					        label="详细地址" 
					        placeholder="请填写小区、街道、门牌号等信息"
					        :field-style="{flex: 1, 'margin-left': '20rpx', height: '160rpx'}"
					        />
					    </view>
						<view class="form-item">
						    <view class="label">冰柜码</view>
						    <input v-model="form.freezer_sn" :placeholder-style="placeholderStyle" type="number" placeholder="请填写冰柜编码"></input>
						</view>
						<view class="form-item">
						    <view class="label">店铺定位</view>
							<view class="form-input" @tap="openLocation">
								<image src="/static/images/location.png"></image>
								<view class="input-label">{{location?'已授权位置':'授权地理位置'}}</view>
							</view>
						</view>
					</view>
				</view>
				<view class="card">
					<view class="card_header">
						<view class="head_title">公司资质</view>
						<view class="head_tips">三证合一</view>
					</view>
					<view class="card_info">
						<view class="card_box">
							<yh-uploadimage :token="token" upload="/static/images/license@2x.png"
								:url="form.license_img" @success="licenseUpload"></yh-uploadimage>
							<view class="box_text">上传营业执照</view>
						</view>
				
					</view>
				</view>
				<view class="card">
					<view class="card_header">
						<view class="head_title">法人实名认证</view>
					</view>
					<view class="card_info">
						<view class="card_box">
							<yh-uploadimage :token="token" upload="/static/images/fristcard@2x.png"
								:url="form.idcard_front" @success="fristUpload"></yh-uploadimage>
							<view class="box_text">身份证正面</view>
						</view>
						<view class="card_box">
							<yh-uploadimage :token="token" upload="/static/images/lastcard@2x.png"
								:url="form.idcard_back" @success="lastUpload"></yh-uploadimage>
							<view class="box_text">身份证背面</view>
						</view>
					</view>
				</view>
				<view class="card">
					<view class="card_header">
						<view class="head_title">店铺图片</view>
					</view>
					<view class="card_info">
						<view class="card_box">
							<yh-uploadimage :token="token"
								:url="form.business_img" @success="businessUpload"  width="160" height="160"></yh-uploadimage>
							<view class="box_text">店铺图片</view>
						</view>
					</view>
				</view>
				<view class="agreement mb20">
					<u-checkbox v-model="agreement" size="36" active-color="#FF2C3C" shape="circle"></u-checkbox>
					<view class="sm row">
						已阅读并同意
						<navigator class="primary" hover-class="none" url="/pages/bundle/server_explan/server_explan?type=4">《申请商家合同协议》</navigator>
					</view>
				</view>
				<view class="commonBtn" @tap="submitEvent">提交</view>
			</view>
		</view>
		
		<u-select :safe-area-inset-bottom="true"  mode="mutil-column-auto" value-name="value" label-name="label" child-name="children" @confirm="regionChange" :list="area" v-model="showRegion"></u-select>
	</view>
</template>

<script>
	import {businessApply,businessApplyInfo} from '@/api/common.js'
	import {mapGetters} from 'vuex'
	import area from '@/utils/area.js'
	export default {
		data() {
			return {
				form:{
					id: 0,
					province_id:0,
					city_id:0,
					district_id:0,
					region:'',
				},
				showRegion: false,
				agreement:false,
				area: [],
				placeholderStyle: 'font-size: 28rpx;fontfamily:PingFang SC;color: #808080;',
				noticelist: [
					'隐私说明：请按要求上传真实的身份信息，您所上传的信息仅用于平台账户绑定',
					'请放心上传，否则带来的一切问题后果自负'
				],
			}
		},
		computed: {
			...mapGetters(['token']),
			location(){
				if(uni.getStorageSync('location')){
					return true
				}else{
					return false
				}
			}
		},
		onLoad() {
			this.$nextTick(() => {
				this.area = area
			})
			this.businessApplyInfo()
		},
		methods: {
			openLocation(){
				uni.navigateTo({
					url:'/pages/location/location'
				})
			},
			regionChange(region) {
				this.form.province_id = region[0].value;
				this.form.city_id = region[1].value;
				this.form.district_id = region[2].value;
				this.form.region = region[0].label + " " + region[1].label + " " + region[2].label
			},
			businessApplyInfo() {
				businessApplyInfo().then(res => {
					if (res.code == 1) {
						if(res.data){
							this.form = res.data;
							if(res.data.province && res.data.city && res.data.district){
								this.form.region = `${res.data.province} ${res.data.city} ${res.data.district}`
							}
						}
					}
				})
			},
			licenseUpload(url) {
				this.form.license_img = url;
			},
			fristUpload(url) {
				this.form.idcard_front = url;
			},
			lastUpload(url) {
				this.form.idcard_back = url;
			},
			businessUpload(url){
				this.form.business_img = url;
			},
			// 提交申请
			submitEvent: function() {
				if(!this.agreement) return this.$toast({title: '请阅读并勾选申请商家合同协议'})
				const reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
				const value = this.form;
				let location = uni.getStorageSync('location')
				value.lat = location.latitude;
				value.lng = location.longitude;
				if (!value.name) return this.$toast({
					title: '请填写姓名'
				});
				if (!value.mobile) return this.$toast({
					title: '请填写手机号码'
				});
				if (!reg.test(value.mobile)) return this.$toast({
					title: '请填写正确身份证号'
				});
				if (!value.region) return this.$toast({
					title: '请选择省、市、区'
				});
				if (!value.address) return this.$toast({
					title: '请填写详细地址'
				});
				if (!value.business_name) return this.$toast({
					title: '请填写门店名称'
				});
				if (!value.license_img) return this.$toast({
					title: '请上传营业执照'
				});
				if (!value.idcard_front) return this.$toast({
					title: '请上传身份证正面'
				});
				if (!value.idcard_back) return this.$toast({
					title: '请上传身份证反面'
				});
				if (!value.business_img) return this.$toast({
					title: '请上传店铺图片'
				});
				if (!value.lat) return this.$toast({
					title: '您拒绝授权位置信息，请重新授权'
				});
				if (!value.lng) return this.$toast({
					title: '您拒绝授权位置信息，请重新授权'
				});
				businessApply(value).then(res => {
					if (res.code == 1) {
						this.businessApplyInfo()
						this.$toast({
							title: '申请成功'
						});
					} else {
						this.$toast({
							title: res.message
						});
					}
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.content_box{
		.merchant_enter {
			padding-bottom: 100rpx;
			.cell_box {
				margin: 20rpx 25rpx;
				background-color: #FFFFFF;
				border-radius: 20rpx;
				
				.form {
				    flex: 1;
					border-radius: 20rpx;
				    .form-item {
						display: flex;
						justify-content: space-between;
						align-items: center;
				        padding: 0 24rpx;
				        height: 80rpx;
				        &:not(:nth-of-type(3)) {
				            border-bottom: $-solid-border;
				        }
				        .label {
				            width: 150rpx;
				        }
						input {
						    height: 100%;
						    flex: 1;
						}
				        .region{
							width: 70%;
				        	font-size: 28rpx;
				        	font-family: PingFang SC;
				        	font-weight: 500;
				        	color: #808080;
				        	line-height: 40rpx;
				        }
						.form-input{
							display: flex;
							justify-content: space-between;
							align-items: center;
							image{
								width: 60rpx;
								height: 60rpx;
							}
							.input-label{
								font-size: 24rpx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #00aaff;
								line-height: 40rpx;
							}
						}
				    }
				}
			
			}
		}
		.card {
			margin: 20rpx 25rpx;
			padding: 30rpx 30rpx;
			background: #FFFFFF;
			border-radius: 10px;
			.card_header {
				display: flex;
				justify-content: flex-start;
				margin-bottom: 30rpx;
				.head_title {
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #1D1F24;
					line-height: 54rpx;
				}
				.head_tips {
					margin-left: 30rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #1D1F24;
					line-height: 54rpx;
				}
			}
			.card_info {
				display: flex;
				justify-content: flex-start;
				.card_box{
					margin-right: 20rpx;
					.box_text {
						margin-top: 20rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #4D4D4D;
						text-align: center;
						line-height: 40rpx;
					}
				}
				
			}
		}
		.agreement{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 10rpx 25rpx;
		}
		.commonBtn {
			margin: 0 25rpx;
			margin-top: 50rpx;
			width: 94%;
			height: 98rpx;
			background: #EA4445;
			border-radius: 49rpx;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			text-align: center;
			line-height: 98rpx;
		}
	}
</style>